// pages/FeiSu/addMsg/addMsg.js
import encode from '../../../utils/base64.js';
import request from '../../../utils/config.js';
import util from '../../../utils/util.js';
let WXBizDataCrypt = require('../../../utils/RdWXBizDataCrypt.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fieldsIndex: 'none',
    modesIndex: 'none',
    lawyerIndex: 'none',
    index: 0,
    fields:[],
    modes: [],
    lawyers:[],
    serviceDate: util.timetrans(new Date(), 'day'),
    serviceContent: '',
    serviceDuration: '',
    memo: '',
    nextPlan:'',
    projectId:'',
    projectTypeId:''
  },

  lawyerChange: function (e) {
    this.setData({
      lawyerIndex: e.detail.value
    })
  },
  modesChange: function (e) {
    this.setData({
      modesIndex: e.detail.value
    })
  },
  fieldsChange: function (e) {
    this.setData({
      fieldsIndex: e.detail.value
    })
  },
  dateChange: function (e) {
    this.setData({
      serviceDate: e.detail.value
    })
  },
  serviceContentChange: function (e) {
    this.setData({
      serviceContent: e.detail.value
    })
  },
  serviceDurationChange: function (e) {
    this.setData({
      serviceDuration: e.detail.value
    })
  },
  memoChange: function (e) {
    this.setData({
      memo: e.detail.value
    })
  },
  nextPlanChange: function (e) {
    this.setData({
      nextPlan: e.detail.value
    })
  },
  changeValue_memo: function (e) {
    console.log(e)
    this.setData({
      memo: e.detail
    })
  },
  changeValue_serviceContent: function (e) {
    console.log(e)
    this.setData({
      serviceContent: e.detail
    })
  },
  changeValue_nextPlan: function (e) {
    console.log(e)
    this.setData({
      nextPlan: e.detail
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    let lawyers = JSON.parse(options.lawyers), lawyerIndex='none';
    for (let i = 0; i < lawyers.length; i++) {
      if (lawyers[i].lawyerRole=='1') {
        lawyerIndex = i;
        break;
      }
    }
    for (let i = 0; i < lawyers.length;i++){
      if (app.globalData.operId == lawyers[i].lawyerId){
        lawyerIndex=i;
        break;
      }
    }
    this.setData({ lawyers: lawyers, lawyerIndex: lawyerIndex, projectId: options.projectId, projectTypeId: options.projectTypeId})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getfields();
    this.getmodes()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },
  ok:function(){
    if (this.data.fieldsIndex=='none'){
      wx.showToast({
        title: '请选择业务领域',
        icon: 'none',
      })
      return;
    }
    if (this.data.modesIndex == 'none') {
      wx.showToast({
        title: '请选择服务方式',
        icon: 'none',
      })
      return;
    }
    if (!this.data.serviceContent) {
      wx.showToast({
        title: '请输入服务内容',
        icon: 'none',
      })
      return;
    }
    if (!this.data.serviceDuration) {
      wx.showToast({
        title: '请输入服务时间',
        icon: 'none',
      })
      return;
    } else if (isNaN(this.data.serviceDuration)){
      wx.showToast({
        title: '服务时间格式错误',
        icon: 'none',
      })
      return;
    }
    if (this.data.lawyerIndex == 'none') {
      wx.showToast({
        title: '请选择承办律师',
        icon: 'none',
      })
      return;
    }
    if (!this.data.serviceDate) {
      wx.showToast({
        title: '请选择服务时间',
        icon: 'none',
      })
      return;
    }
    let send={
      serviceFieldId: this.data.fields[this.data.fieldsIndex].serviceFieldId,
      serviceFieldName: this.data.fields[this.data.fieldsIndex].serviceFieldName,
      serviceModeId: this.data.modes[this.data.modesIndex].serviceModeId,
      serviceModeName: this.data.modes[this.data.modesIndex].serviceModeName,
      serviceContent: this.data.serviceContent,
      serviceDuration: this.data.serviceDuration,
      lawyerId: this.data.lawyers[this.data.lawyerIndex].lawyerId,
      lawyerName: this.data.lawyers[this.data.lawyerIndex].lawyerName,
      serviceDate: this.data.serviceDate ? this.data.serviceDate.replace(/-/g,''):'',
      projectId: this.data.projectId
    }
    if (this.data.memo){
      send.memo = this.data.memo
    }
    if (this.data.nextPlan) {
      send.nextPlan = this.data.nextPlan
    }
    console.log(send)
    let _this = this;
    wx.showLoading({ title: ' ', mask: true })
    request.requestMisApi("post", "projectServicesRecord", send, (res) => {
      console.log("添加", res)
      if (res.code == '00') {
        wx.hideLoading()
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 2000
        })
        if (app.globalData.listRefresh.FS.tabtype =='service'){
          app.globalData.listRefresh.FS = {
            refresh: true,
          }
        }
        setTimeout(function () {
          wx.navigateBack();
        }, 1000);
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000
        })
      }
    }, (res) => {
      console.log("获取失败", res)
    });
  },
  back:function(){
    wx.navigateBack({
      
    })
  },
  getfields:function(){
    let _this = this;
    request.requestMisApi("get", "projectServices/fields", {}, (res) => {
      console.log("获取", res)
      if (res && res.length>0) {
        _this.setData({
          fields: res
        });
      }
    }, (res) => {
      console.log("获取失败", res)
    });
  },
  getmodes: function () {
    let _this = this;
    request.requestMisApi("get", "projectServices/modes", {}, (res) => {
      if (res && res.length > 0) {
        console.log("获取", res)
        _this.setData({
          modes: res
        });
      }
    }, (res) => {
      console.log("获取失败", res)
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
  
  // }
})